package com.older.wptsb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.older.wptsb.domain.dto.OutingRecordDTO;
import com.older.wptsb.domain.dto.UpdateOutingRecordDTO;
import com.older.wptsb.domain.po.OutingRecord;
import com.older.wptsb.domain.po.User;
import com.older.wptsb.domain.vo.OutingRecordVO;
import com.older.wptsb.mapper.CommunityOutingMapper;
import com.older.wptsb.mapper.UserMapper;
import com.older.wptsb.service.CommunityOutingService;
import com.older.wptsb.util.UserContext;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

@Service
public class CommunityOutingServiceImpl extends ServiceImpl<CommunityOutingMapper, OutingRecord> implements CommunityOutingService {

    @Resource
    private CommunityOutingMapper communityOutingMapper;
    @Resource
    private UserMapper userMapper;
    @Override
    public List<OutingRecordVO> getOutingRecord() {
        Long currentUserId = UserContext.getUserId();
        System.out.println(currentUserId);
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("user_id", currentUserId));
        System.out.println(user);
        return communityOutingMapper.selectOutingRecord(user.getCommunityId());
    }

    @Override
    public void addOutingRecord(OutingRecordDTO outingRecordDTO) {
        // 转换DTO到实体类
        OutingRecord outingRecord = new OutingRecord();
        BeanUtils.copyProperties(outingRecordDTO, outingRecord);
        Long currentUserId = UserContext.getUserId();
        outingRecord.setOperatorId(Math.toIntExact(currentUserId));
        // 保存到数据库
        communityOutingMapper.insert(outingRecord);
    }

    @Override
    public void updateOutingRecord(UpdateOutingRecordDTO updateOutingRecordDTO) {
        System.out.println(updateOutingRecordDTO);
        OutingRecord outingRecord = communityOutingMapper.selectOne(new QueryWrapper<OutingRecord>().eq("record_id",updateOutingRecordDTO.getRecordId()));
        outingRecord.setReturnTime(updateOutingRecordDTO.getReturnTime());
        outingRecord.setNotes(updateOutingRecordDTO.getNotes());
        outingRecord.setDirection(updateOutingRecordDTO.getDirection());
        communityOutingMapper.updateOutingRecord(outingRecord);

    }
}
